﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Estimate.Common.Estimate;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Entity;
using Estimate.Common.DataFileFinder;
using Estimate.Reports;

namespace EstimateService
{
    /// <summary>
    /// 程序执行状态数据获取接口
    /// </summary>
    public class HandlerExecutionStatusReport : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string tid = context.Request.QueryString["tid"] != null ? context.Request.QueryString["tid"].ToString() : "";
            string action = context.Request.QueryString["action"] != null ? context.Request.QueryString["action"].ToString() : "";

            //获取回调函数名
            string callback = context.Request.QueryString["callback"];
            //json数据
            string json = "";
  
            if (action == "delete")
                json = DeleteExecutionStatus(tid).ToString();
            else
                json = GetExecutionStatus(tid);

            context.Response.ContentType = "application/json";
            //输出：回调函数名(json数据)
            context.Response.Write(callback + "(" + json + ")");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 获取程序执行状态
        /// </summary>
        /// <param name="tid">任务号</param>
        /// <returns></returns>

        public string GetExecutionStatus(string tid)
        {
            ExecutionStatus exeStatus = new ExecutionStatus(tid);
            return exeStatus.GetStatusFromCache(tid);
        }

        /// <summary>
        /// 从缓存中删除程序执行状态
        /// </summary>
        /// <param name="tid">任务号</param>
        /// <returns></returns>

        public bool DeleteExecutionStatus(string tid)
        {
            ExecutionStatus exeStatus = new ExecutionStatus(tid);
            return exeStatus.DeleteStatusFromCache(tid);
        }


    }
}